﻿using QLXN.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLXN.Product
{
    public partial class ProductEdit : hTekForm
    {
        public ProductEdit(string username)
        {
            InitializeComponent();
            txt_us.Text = txt_last_modify.Text = username;
        }

        public ProductEdit(string id, string username)
        {
            InitializeComponent();
            txt_id.Text = id;
            txt_id.ReadOnly = true;
            txt_us.Text = username;
        }

        SqlInteraction sql = new SqlInteraction();

        private void ProductEdit_Load(object sender, EventArgs e)
        {
            load_manufacturer_list();
            if (txt_id.Text != "")
                load_product();
        }

        private void load_manufacturer_list()
        {
            try
            {
                string query = "SELECT * FROM qlxn_manufacturers ORDER BY name";
                DataTable dt = new DataTable();
                dt = sql.Execute_Table(query);
                txt_manufacturer.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void load_product()
        {
            try
            {
                string id = txt_id.Text;
                string query = "SELECT qlxn_products.id, qlxn_products.name, qlxn_manufacturers.name AS manufacturer, qlxn_products.unit, qlxn_products.detail, ISNULL(qlxn_products.stored, 0) AS stored, qlxn_products.last_modify, qlxn_products.last_modify_time FROM qlxn_products INNER JOIN qlxn_manufacturers ON qlxn_products.manufacturer = qlxn_manufacturers.id AND qlxn_products.id = '" + id + "'";
                DataTable dt = new DataTable();
                dt = sql.Execute_Table(query);
                if (dt.Rows.Count > 0)
                {
                    txt_name.Text = dt.Rows[0]["name"].ToString();
                    string manufacturer = dt.Rows[0]["manufacturer"].ToString();
                    txt_manufacturer.SelectedIndex = txt_manufacturer.FindStringExact(manufacturer);
                    txt_unit.Text = dt.Rows[0]["unit"].ToString();
                    txt_stored.Text = dt.Rows[0]["stored"].ToString();
                    txt_detail.Text = dt.Rows[0]["detail"].ToString();
                    txt_last_modify.Text = dt.Rows[0]["last_modify"].ToString();
                    string t = dt.Rows[0]["last_modify_time"].ToString();
                    if (t != "")
                    {
                        DateTime time = Convert.ToDateTime(t);
                        txt_last_modify_time.Text = time.ToString("HH:mm dd/MM/yyyy");
                    }
                    else
                        txt_last_modify_time.Text = "00:00 00/00/0000";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                string id = txt_id.Text;
                if (id.Length > 2)
                {
                    string name = txt_name.Text;
                    string manufacturer = txt_manufacturer.SelectedValue.ToString();
                    string unit = txt_unit.Text;
                    string stored = txt_stored.Text;
                    string detail = txt_detail.Text;
                    string last_modify = txt_us.Text;
                    DateTime last_modify_time = DateTime.Now;
                    string query = "";
                    if (!txt_id.ReadOnly)
                        query = "INSERT INTO qlxn_products(id, name, manufacturer, unit, stored, detail, last_modify, last_modify_time) VALUES('" + id + "', N'" + name + "', " + manufacturer + ", N'" + unit + "', " + stored + ", N'" + detail + "', '" + last_modify + "', '" + last_modify_time + "')";
                    else
                        query = "UPDATE qlxn_products SET name = N'" + name + "', manufacturer = " + manufacturer + ", unit = N'" + unit + "', stored = " + stored + ", detail = N'" + detail + "', last_modify = '" + last_modify + "', last_modify_time = '" + last_modify_time + "' WHERE id = '" + id + "'";
                    bool flag = sql.Execute_Sql(query);
                    if (flag)
                    {
                        MessageBox.Show(this, "Lưu dữ liệu thành công!" + Environment.NewLine, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                    MessageBox.Show(this, "Mã số phải từ 3 kí tự trở lên!" + Environment.NewLine, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("PRIMARY KEY"))
                    MessageBox.Show(this, "Mã số đã tồn tại, chọn mã khác!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                else
                    MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
